package mo.offer_special;

public class L041 {

    private int size;
    private int[] elements;
    private int allSize;
    private int sum = 0;

    /** Initialize your data structure here. */
    public L041(int size) {
        this.size = size;
        elements = new int[size];
    }

    public double next(int val) {
        allSize++;
        sum += val;
        int pos = (allSize - 1) % size;
        if (allSize > size) {
            sum -= elements[pos];
        }
        elements[pos] = val;
        return ((double) sum) / (allSize > size ? size : allSize);
    }

    public static void main(String[] args) {
        L041 test = new L041(3);
        System.out.println(test.next(1));
        System.out.println(test.next(10));
        System.out.println(test.next(3));
        System.out.println(test.next(5));
    }

}
